﻿using BW.IBLL;
using BW.IDAL;
using BW.Model;

namespace BW.BLL
{
    public class BaseBLL:IBaseBLL
    {
        IBaseDAL<MombetModel> del;
        public BaseBLL(IBaseDAL<MombetModel> del)
        {
            this.del = del;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> Add(MombetModel t)
        {
            return await del.GetAdd(t);
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<MombetModel> Find(int id)
        {
            return await del.GetValue(x=>x.Id == id);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="totalcount"></param>
        /// <param name="totalpage"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IQueryable<MombetModel> GetShow(out int totalcount, out int totalpage, int pageindex = 1, int pagesize = 3)
        {
            var list = del.GetAll();
            totalcount = list.Count();
            totalpage = (int)Math.Ceiling(totalcount * 1.0 / pagesize);
            list=list.OrderBy(x => x.Id).Skip((pageindex-1)*pagesize).Take(pagesize);
            return list;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public  async Task<dynamic> Login(string username, string pwd)
        {
            var list =await del.GetValue(x=>x.Name == username);
            if(list == null)
            {
                return -1;
            }
            else
            {
                if(list.Pwd!=pwd)
                {
                    return -2;
                }
                else
                {
                    return list;
                }
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> update(MombetModel t)
        {
            return await del.GetUpdate(t);
        }
    }
}
